package com.zzh.jn_erp.common;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzh.jn_erp.common.util.ApplicationContextUtil;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public abstract class CommonSqlSessionSupport {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    public CommonSqlSessionSupport() {
    }

    public SqlSession getSqlSession() {
        return (SqlSession) ApplicationContextUtil.getBean("sqlSessionTemplate", SqlSessionTemplate.class);
    }

    public SqlSession getSqlSession(String beanPrefix) {
        return (SqlSession)ApplicationContextUtil.getBean(beanPrefix + "SqlSessionTemplate", SqlSessionTemplate.class);
    }

    public <E> List<E> list(String statement, Object parameter) {
        return this.getSqlSession().selectList(statement, parameter);
    }

    public <E> List<E> list(SqlSession sqlSession, String statement, Object parameter) {
        return sqlSession.selectList(statement, parameter);
    }

    public <E> List<E> list(String statement) {
        return this.getSqlSession().selectList(statement);
    }

    public <E> List<E> list(SqlSession sqlSession, String statement) {
        return sqlSession.selectList(statement);
    }

    public <T> T one(String statement, Object parameter) {
        return this.getSqlSession().selectOne(statement, parameter);
    }

    public <T> T one(SqlSession sqlSession, String statement, Object parameter) {
        return sqlSession.selectOne(statement, parameter);
    }

    public int add(String statement, Object parameter) {
        return this.getSqlSession().insert(statement, parameter);
    }

    public int add(SqlSession sqlSession, String statement, Object parameter) {
        return sqlSession.insert(statement, parameter);
    }

    public int sub(String statement, Object parameter) {
        return this.getSqlSession().delete(statement, parameter);
    }

    public int sub(SqlSession sqlSession, String statement, Object parameter) {
        return sqlSession.delete(statement, parameter);
    }

    public int mod(String statement, Object parameter) {
        return this.getSqlSession().update(statement, parameter);
    }

    public int mod(SqlSession sqlSession, String statement, Object parameter) {
        return sqlSession.update(statement, parameter);
    }

    public <E> Page<E> page(String statement, Object parameter, boolean total) {
        return this._page(this.getSqlSession(), statement, parameter, total);
    }

    public <E> Page<E> page(SqlSession sqlSession, String statement, Object parameter, boolean total) {
        return this._page(sqlSession, statement, parameter, total);
    }

    private <E> Page<E> _page(SqlSession sqlSession, String statement, Object parameter, boolean total) {
        PageInfo<E> p = null;
        List result = null;

        try {
            if (parameter instanceof Map) {
                Map<?, ?> _map = (Map)parameter;
                PageHelper.startPage(Integer.parseInt(_map.get("pageNum").toString()), this._maxPageSize(Integer.parseInt(_map.get("pageSize").toString())), total);
            } else {
                CommonBaseEntity commonBaseEntity = (CommonBaseEntity)parameter;
                PageHelper.startPage(commonBaseEntity.getPageNum(), this._maxPageSize(commonBaseEntity.getPageSize()), total);
            }

            result = sqlSession.selectList(statement, parameter);
            p = new PageInfo(result);
            Page var14 = (Page)((Page)p.getList());
            return var14;
        } catch (Exception var11) {
            this.log.error("获取自动分页数据出现异常，异常原因是【{}】", var11.getMessage());
        } finally {
            p = null;
            result = null;
        }

        return new Page();
    }

    public <E> List<E> page(String statement, Object parameter, int pageSize) {
        return this._page(this.getSqlSession(), statement, parameter, pageSize);
    }

    public <E> List<E> page(SqlSession sqlSession, String statement, Object parameter, int pageSize) {
        return this._page(sqlSession, statement, parameter, pageSize);
    }

    private <E> List<E> _page(SqlSession sqlSession, String statement, Object parameter, int pageSize) {
        PageInfo<E> p = null;
        List result = null;

        try {
            PageHelper.startPage(1, this._maxPageSize(pageSize), false);
            result = sqlSession.selectList(statement, parameter);
            p = new PageInfo(result);
            List var7 = p.getList();
            return var7;
        } catch (Exception var11) {
            this.log.error("获取自动分页数据出现异常,异常原因是【{}】", var11.getMessage());
        } finally {
            p = null;
            result = null;
        }

        return new ArrayList();
    }

    public <E> List<E> page(String statement, Object parameter, int pageNum, int pageSize) {
        return this._page(this.getSqlSession(), statement, parameter, pageNum, pageSize);
    }

    public <E> List<E> page(SqlSession sqlSession, String statement, Object parameter, int pageNum, int pageSize) {
        return this._page(sqlSession, statement, parameter, pageNum, pageSize);
    }

    private <E> List<E> _page(SqlSession sqlSession, String statement, Object parameter, int pageNum, int pageSize) {
        PageInfo<E> p = null;
        List result = null;

        try {
            PageHelper.startPage(pageNum, this._maxPageSize(pageSize), false);
            result = sqlSession.selectList(statement, parameter);
            p = new PageInfo(result);
            List var8 = p.getList();
            return var8;
        } catch (Exception var12) {
            this.log.error("获取自动分页数据出现异常,异常原因是【{}】", var12.getMessage());
        } finally {
            p = null;
            result = null;
        }

        return new ArrayList();
    }

    public <E> Page<E> page(String statement, Object parameter, int pageNum, int pageSize, boolean total) {
        return this._page(this.getSqlSession(), statement, parameter, pageNum, pageSize, total);
    }

    public <E> Page<E> page(SqlSession sqlSession, String statement, Object parameter, int pageNum, int pageSize, boolean total) {
        return this._page(sqlSession, statement, parameter, pageNum, pageSize, total);
    }

    private <E> Page<E> _page(SqlSession sqlSession, String statement, Object parameter, int pageNum, int pageSize, boolean total) {
        PageInfo<E> p = null;
        List result = null;

        try {
            PageHelper.startPage(pageNum, pageSize, total);
            result = sqlSession.selectList(statement, parameter);
            p = new PageInfo(result);
            Page var9 = (Page)((Page)p.getList());
            return var9;
        } catch (Exception var13) {
            this.log.error("获取自动分页数据出现异常,异常原因是【{}】", var13.getMessage());
        } finally {
            p = null;
            result = null;
        }

        return new Page();
    }

    private int _maxPageSize(int _size) {
        return _size > 500 ? 500 : _size;
    }
}
